layui.use(['jquery', 'element', 'form', 'layer'], function () {
    var $ = layui.jquery;
    var element = layui.element;
    var form = layui.form;
    var layer = layui.layer;
    //获取微圈id
    var microcirclerd = localStorage.getItem("microcircleId");
    //使用人id
    var createId = localStorage.getItem('createId');
    //发布评论
    $('#postComments').click(function () {
        var content = $("#content").val();
        if (content == '') {
            layer.msg("评论不能为空");
        } else {
            var comment = {
                userId: createId,
                commodityId: microcirclerd,
                type: 4,
                userType: '2',
                status: '1',
                content: content,
                parentId: 0,
                display: 2,
                fabulous: 0,
                reply: 0,
                createId: createId,
            }
            $.ajax({
                url: "/comment/addComment",
                type: "post",
                dataType: "json",
                data: comment,
                success: function (data) {
                    if (data.data) {
                        layer.msg("评论成功");
                        $("#content").val('');
                        getcomment();
                    } else {
                        layer.msg("评论失败");
                    }
                }
            })
        }
    });
    //全部评论
    getcomment();

    function getcomment() {
        $.ajax({
            url: "/comment/getCommentById",
            type: "post",
            dataType: 'json',
            data: {'id': microcirclerd, 'type': 4},
            success: function (data) {
                var commentData = data.data;
                if (commentData.length == 0) {
                    $('#table').html("<tr>" +
                        "<td><img src='/images/moren.png' class='layui-img'></td>" +
                        "<td><p>还没有评论...</p></td></tr>");
                } else {
                    for (var i = 0; i < commentData.length; i++) {
                        $('#table').append("<tr>" +
                            "<td><img src='http://" + commentData[i].acatarUrl + "'  class='layui-img'/></td>" +
                            "<td><p class='layui-color'>" + commentData[i].trueName + "</p></td>" +
                            "<td><p>" + commentData[i].content + "</p></td>" +
                            "<td><p class='layui-color'>" + commentData[i].createDate + "</p></td>/tr>");
                    }
                }
            }
        });
    }
});